<?php

declare (strict_types=1);

namespace app\admin\middleware;

use app\model\SystemUserModel;
use Closure;

/**
 * 管理员身份验证
 * Class CheckAdmin
 * @package app\middleware
 */
class CheckSystemUser
{
    public function handle($request, Closure $next)
    {
        if ($request->pathinfo() == 'Login/login') {
            return $next($request);
        }
        // JWT用户令牌认证，令牌内容获取
        if(empty($request->header('authorization'))){
            myException('无权限');
        }
        $userToken = str_replace('Bearer ', '', $request->header('authorization'));
        $result    = (new SystemUserModel())->checkToken($userToken);
        if ($result['code']) {
            $request->user = $result['data'];

            return $next($request);
        }

        return $result['data'];
    }
}
